<?php
/**
 *
 * PHP versions 5
 *
 * Copyright (C)
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
 * of ERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software
 * Fundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
 *
 * @author Bastian Schröder <bs@chemanet.de>
 * @version 2007-02-10
 * @package rail
 */
class rail_group extends rail_object {

	/**
	 * Return true if specified user is in this group.
	 *
	 * @exception rail_writepermission_exception
	 * @param rail_user $pRailUser
	 */
	public function add_member ( $pRailUser ) {
		// check write permission
		if( !$this->is_writeable( $this->rail_connector->get_user() ) )
		 	throw new rail_writepermission_exception( $this, $this->rail_connector->get_user() );

		$data = $this->rail_connector->send_request( "SELECT `object1`,`object2` FROM objects_relations WHERE `type`='RAIL_MEMBERSHIP' AND (`object1`='" . $this->get_id() . "' OR `object2`='" . $this->get_id() . "') AND (`object1`='" . $pRailUser->get_id() . "' OR `object2`='" . $pRailUser->get_id() . "') LIMIT 1" )->fetch_assoc();

		// only add if membership dosn't exist.
		if( !count( $data ) > 0 )
			$this->rail_connector->send_request( "INSERT INTO objects_relations (`object1`,`object2`,`type`) VALUES ('" . $this->get_id() . "','" . $pRailUser->get_id() . "','RAIL_MEMBERSHIP')" );
	}


	/**
	 * Return number of group members. Groupmembers are all user in this group.
	 *
	 * @exception rail_readpermission_exception
	 * @return int
	 */
	public function count_members () {
		// check read permission
		if( !$this->is_readable( $this->rail_connector->get_user() ) )
		 	throw new rail_readpermission_exception( $this, $this->rail_connector->get_user() );

		$size = 0;
		$data = $this->rail_connector->send_request( "SELECT type FROM objects_relations WHERE `type`='RAIL_MEMBERSHIP' AND (`object1`='" . $this->get_id() . "' OR `object2`='" . $this->get_id() . "')" );
		while( $data_row = $data->fetch_assoc() )
			$size ++ ;

		// get all objetes has this as group
		$data = $this->rail_connector->send_request("SELECT `id` FROM objects WHERE `type`='rail_user' AND `group`='" . $this->get_id() . "'");
		while( $data_row = $data->fetch_assoc() )
			$size ++ ;

		return $size;
	}


	/**
	 * Return true if specified user is in this group.
	 *
	 * @exception rail_readpermission_exception
	 * @param rail_user $pRailUser
	 * @return bool
	 */
	public function is_member ( $pRailUser ) {
		// check read permission
		if( !$this->is_readable( $this->rail_connector->get_user() ) )
		 	throw new rail_readpermission_exception( $this, $this->rail_connector->get_user() );

		 // check if this group is the primary group for specofied rail_user
		 if( $pRailUser->get_owner_group()->get_id() == $this->get_id() )
		 	return true;

		 // get all RAIL_MEMBERSHIP relations from user for this group
		 // return true if one or more relations found
		$data = $this->rail_connector->send_request( "SELECT `object1`,`object2` FROM objects_relations WHERE `type`='RAIL_MEMBERSHIP' AND (`object1`='" . $this->get_id() . "' OR `object2`='" . $this->get_id() . "') AND (`object1`='" . $pRailUser->get_id() . "' OR `object2`='" . $pRailUser->get_id() . "') LIMIT 1" )->fetch_assoc();
		if( count( $data ) > 0 )
			return true;

		// get all objetes has this as group
		// returned true if pRailUser has this group as primary group
		$data = $this->rail_connector->send_request("SELECT `id` FROM objects WHERE `id`='" . $pRailUser->get_id() . "' `type`='rail_user' AND `group`='" . $this->get_id() . "'")->fetch_assoc();
		if( count( $data ) > 0 )
			return true;

		return false;
	}


	/**
	 * Get all members in this group. Returned array of rail_users.
	 *
	 * @exception rail_readpermission_exception
	 * @return array
	 */
	public function get_members () {
		// check read permission
		if( !$this->is_readable( $this->rail_connector->get_user() ) )
		 	throw new rail_readpermission_exception( $this, $this->rail_connector->get_user() );

		$objects = array();

		// get all objects has an RAIL_MEMBERSHIP relation to this group
		$data = $this->rail_connector->send_request( "SELECT `object1`,`object2` FROM objects_relations WHERE `type`='RAIL_MEMBERSHIP' AND (`object1`='" . $this->get_id() . "' OR `object2`='" . $this->get_id() . "') ORDER BY object1,object2" );
		while( $data_row = $data->fetch_assoc() ){
			$object_id	=  ($data_row[ "object1" ] == $this->get_id())?$data_row[ "object2" ]:$data_row[ "object1" ];
			$objects[] = rail_factory::get_object( $this->rail_connector, $object_id );
		}

		// get all objetes has this as group
		$data = $this->rail_connector->send_request("SELECT `id` FROM objects WHERE `type`='rail_user' AND `group`='" . $this->get_id() . "'");
		while( $data_row = $data->fetch_assoc() )
			$objects[] = rail_factory::get_object( $this->rail_connector, $data_row[ "id" ] );

		// return
		return $objects;
	}


	/**
	 * Remove a member from this group.
	 *
	 * @exception rail_writepermission_exception
	 * @param rail_user
	 */
	public function remove_member ( $pRailUser ) {
		// check write permission
		if( !$this->is_writeable( $this->rail_connector->get_user() ) )
		 	throw new rail_writepermission_exception( $this, $this->rail_connector->get_user() );

		// get all objects has an RAIL_MEMBERSHIP relation to this group
		$data = $this->rail_connector->send_request( "SELECT `object1`,`object2` FROM objects_relations WHERE `type`='RAIL_MEMBERSHIP' AND (`object1`='" . $this->get_id() . "' OR `object2`='" . $this->get_id() . "') ORDER BY object1,object2" );
		while( $data_row = $data->fetch_assoc() ){
			$object_id	=  ($data_row[ "object1" ] == $this->get_id())?$data_row[ "object2" ]:$data_row[ "object1" ];
			if( $pRailUser->get_id() == $object_id ){
				$this->rail_connector->send_request( "DELETE FROM objects_relations WHERE `type`='RAIL_MEMBERSHIP' AND (`object1`='" . $this->get_id() . "' OR `object2`='" . $this->get_id() . "') AND (`object1`='" . $pRailUser->get_id() . "' OR `object2`='" . $pRailUser->get_id() . "') LIMIT 1" );
				return;
			}
		}

		// if no retrurn parm pRailUser has no relation to this group
		throw new rail_exception($this, $pRailUser->get_name() . " has no relation to this group." );

	}

} // End of rail_group
?>